﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, APIPaging<List<Role>>>
    {
        private readonly IBaseRepository<Role> roleRep;

        public GetRoleCommandHandler(IBaseRepository<Role> roleRep)
        {
            this.roleRep = roleRep;
        }

        public async Task<APIPaging<List<Role>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            APIPaging<List<Role>> result = new APIPaging<List<Role>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = roleRep.GetAll().Where(x => x.IsDel == false);
            result.TotalCount = list.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount*1.0/ request.PageSize);
            list = list.OrderBy(x => x.RoleId).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);
            result.data = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
